package com.diycode.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diycode.entity.Admins;
import com.diycode.mapper.AdminsMapper;
import com.diycode.utils.QueryWrapperUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author diycode
 */
@Service
public class AdminsService extends ServiceImpl<AdminsMapper, Admins> {
    @Resource
    AdminsMapper mapper;

    //删除
    public int delete(Integer id) {
        return mapper.deleteById(id);
    }

    //批量删除
    public int delete(String ids) {
        return mapper.deleteBatchIds(Arrays.asList(ids.split(",")));
    }

    //根据id查询
    public Admins detail(Integer id) {
        return mapper.selectById(id);
    }

    //查询
    public PageInfo<Admins> query(Admins entity) {
        if (entity != null && entity.getPage() != null) {
            PageHelper.startPage(entity.getPage(), entity.getLimit());
        }
        QueryWrapper<Admins> queryWrapper = QueryWrapperUtil.build(entity);
        return new PageInfo<>(mapper.selectList(queryWrapper));
    }

    public Admins getByLogin(String userName, String userPwd) {
        QueryWrapper<Admins> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Admins::getUserName, userName).eq(Admins::getPassWord, userPwd);
        return mapper.selectOne(queryWrapper);
    }
}
